package com.dongzili.web;

import cn.dev33.satoken.annotation.SaIgnore;
import com.dongzili.common.entity.User;
import com.dongzili.common.mybatisjoin.MPJMapperUtil;
import com.dongzili.common.vo.R;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("join")
public class TableJoinController {


    /**
     * 表关联查询
     * @return
     */
    @SaIgnore
    @GetMapping("test")
    public R<?> testJoin(){
        MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(User.class, User::getPassword);
        wrapper.select("b.name as roleName");
        wrapper.select("b.code as roleCode");
        wrapper.leftJoin("json_each(t.roleIds) AS role ON 1=1");
        wrapper.leftJoin("t_role b on b.id = role.value");
        List<Map<String, Object>> list = MPJMapperUtil.getMapper(User.class).selectJoinMaps(wrapper);
        return R.success(list);
    }

    /**
     * 父子关系数据查询
     * @return
     */
    @SaIgnore
    @RequestMapping("sub")
    public R<?> sub(){
        MPJLambdaQueryWrapper<User> wrapper = new MPJLambdaQueryWrapper<>();
        wrapper.selectAll(User.class);
        wrapper.selectIgnore("t.password");
        List<User> list = MPJMapperUtil.getDeepService(User.class).listDeep(wrapper);
        return R.success(list);
    }

}
